// 封装的提示框
const toast = (str, type = 'none') => {
	return new Promise((resolve, reject) => {
		uni.showToast({
			title: str,
			icon: type,
			duration: 2000,
			success: () => {
				if (type != 'none') {
					setTimeout(() => {
						return resolve(1)
					}, 2000)
				}
			}
		})
	})
}
const showModal = ({
	title = '提示',
	content = '确定要提交吗？'
}) => {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: title,
			content: content,
			success: (res) => {
				resolve(res);
			},
			fail: (err) => {
				reject(err);
			}
		})
	})
}

// 处理多次点击
function noClicks(methods) {
	let that = this;

	if (that.noClick) {
		that.noClick = false;
		methods();
		setTimeout(function() {
			that.noClick = true;
		}, 2000)
	} else {
		console.log("请稍后点击")
	}
}

export default {
	toast: toast,
	showModal: showModal,
	noClicks: noClicks
}

// 时间格式化
export function formatDate(val) {
	const date = new Date(val);
	let year = date.getFullYear();
	let month = date.getMonth() + 1; // 月份从0开始，所以需要加1  
	let day = date.getDate();
	let hours = date.getHours();
	let minutes = date.getMinutes();
	let seconds = date.getSeconds();

	// 补0操作  
	month = month < 10 ? '0' + month : month;
	day = day < 10 ? '0' + day : day;
	hours = hours < 10 ? '0' + hours : hours;
	minutes = minutes < 10 ? '0' + minutes : minutes;
	seconds = seconds < 10 ? '0' + seconds : seconds;

	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}